package com.sky.service.impl;

import com.sky.dto.SetmealDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    DishMapper dishMapper;

    /**
     * 条件查询
     * @param setmealDTO
     * @return
     */
    public List<Setmeal> list(SetmealDTO setmealDTO) {
        List<Setmeal> list = setmealMapper.list(setmealDTO);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        //根据套餐id查询绑定的所有菜品关系
        List<SetmealDish> setmealDishes = setmealDishMapper.findBySetmealId(id);

        List<DishItemVO> dishItemList = setmealDishes.stream()
                .map(setmealDish -> {
                    Long dishId = setmealDish.getDishId();
                    //根据菜品id查询菜品信息
                    Dish dish = dishMapper.findById(dishId);
                    //封装DishItem
                    DishItemVO dishItemVO = new DishItemVO();
                    dishItemVO.setImage(dish.getImage());
                    dishItemVO.setDescription(dish.getDescription());
                    dishItemVO.setCopies(setmealDish.getCopies());
                    dishItemVO.setName(setmealDish.getName());
                    return dishItemVO;
                }).collect(Collectors.toList());

        return dishItemList;
    }
}
